package algorithms2.sort;

import java.util.Arrays;

/**
 * @Author: DRevelation
 * @Date: 2021/9/13 10:44
 */
public class BubbleSort {

    public static void main(String[] args) {
        int[] nums = {5, 3, 2, 1, 4};
        int[] sortedNums = bubbleSort(nums);
        System.out.println(Arrays.toString(sortedNums));
    }

    private static int[] bubbleSort(int[] nums) {
        int len = nums.length;
        for (int i = len - 1; i >= 0; i--) {
            for (int j = 0; j < i; j++) {
                if (nums[j] > nums[j + 1]) {
                    Utils.swap(nums, j, j + 1);
                }
            }
            System.out.println("第 " + (len - i) + " 次排序结果是: " + Arrays.toString(nums));
        }
        return nums;
    }
}
